package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.User;
import util.SocketUtil;
import util.StateCode;

public class LoginPanel extends JPanel implements ActionListener, Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton[] bt = new JButton[2];
	private JTextField[] tf = new JTextField[2];
	private boolean isRequest = true;
	private User user;

	public LoginPanel() {
		setLayout(new GridBagLayout());
		setOpaque(false); // 设置透明

		String[] s1 = { "昵  称", "对方IP" }, s2 = { "登陆", "关闭" };
		GridBagConstraints gbc = new GridBagConstraints(); // GridBagConstraints（布局管理器）类
		for (int i = 0; i < s1.length; i++) {
			JLabel label = new JLabel(s1[i]);
			gbc.gridx = 0; // 列（=0是第一列）
			gbc.gridy = i;
			gbc.insets = new Insets(10, 10, 10, 10); // 插入的格子上左下右间距
			add(label, gbc);

			tf[i] = new JTextField(20); // 文本框长度20
			gbc.gridx = 1;
			add(tf[i], gbc);

			bt[i] = new JButton(s2[i]);
			bt[i].addActionListener(this);
			gbc.gridx = i;
			gbc.gridy = 2;
			add(bt[i], gbc);
			gbc.anchor = GridBagConstraints.CENTER;// 默认显示在中间

		}

	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g; // 把画笔（g2）重写 Graphics2D半透明
		Composite composite = g2.getComposite(); // 先保存原画笔属性在composite中
		g2.setColor(Color.gray); // 设置画笔为灰色
		g2.setComposite(AlphaComposite.SrcOver.derive(0.9f)); // 设置透明度为0.9
		g2.fillRect(0, 0, getWidth(), getHeight()); // 填充坐标0，0 大小getWidth(),
													// getHeight()
		g2.setComposite(composite); // 把画笔属性还原
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == bt[0]) {
			String name = tf[0].getText();
			String ip = tf[1].getText();
			if (name.equals("") || ip.equals("")) {
				JOptionPane.showMessageDialog(this, "请输入昵称和IP！");
				return;
			}
			user = SocketUtil.getUser();
			user.setName(name);
			user.setIp(ip);
			if (isRequest) {
				try {
					Socket socket = new Socket(ip, 6666);
					SocketUtil.setSocket(socket);
					SocketUtil.sendMessage(StateCode.QINGQIULIANJIE);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			} else {
				this.setVisible(false);
				SocketUtil.sendMessage(user);
			}
		} else {
			System.exit(0);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof Integer) {
			int code = (int) arg;
			switch (code) {
			case StateCode.QINGQIULIANJIE:
				int f = JOptionPane.showConfirmDialog(this, "有人请求联机，同意吗？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.sendMessage(StateCode.TONGYILIANJIE);
					isRequest = false;
					tf[1].setText(SocketUtil.getSocket().getInetAddress().getHostAddress());
					tf[1].setEnabled(false);
				} else {
					SocketUtil.sendMessage(StateCode.JUJUELIANJIE);
				}
				break;
			case StateCode.TONGYILIANJIE:
				this.setVisible(false);
				SocketUtil.sendMessage(user);
				break;
			case StateCode.JUJUELIANJIE:
				JOptionPane.showMessageDialog(this, "对方拒绝了请求1");
				break;
			case StateCode.LIANJIECHONGZHI:
				SocketUtil.startServer();
				this.setVisible(true);
				break;
			}
		}
	}

}